<template>
    <div>
        <el-table border :data="skuList">
            <el-table-column label="序号" type="index" align="center" width="80px"></el-table-column>
            <el-table-column label="名称" prop="skuName"></el-table-column>
            <el-table-column label="描述" prop="skuDesc"></el-table-column>

            <el-table-column label="默认图片" width="110px">
                <template slot-scope="{row,$index}">
                    <img :src="row.skuDefaultImg" alt="" width="80px" height="80px">
                </template>
            </el-table-column>

            <el-table-column label="重量" width="80px" prop="weight" size="mini"></el-table-column>
            <el-table-column label="价格" width="80px" prop="price" size="mini"></el-table-column>

            <el-table-column label="操作">
                <template slot-scope="{row,$index}">
                    <HintButton @click="sale(row)" v-if="row.isSale==0" title="下架" type="success" icon="el-icon-sort-down" size="mini" style="margin-right: 5px"></HintButton>
                    <HintButton @click="cancel(row)" v-else title="上架" type="success" icon="el-icon-sort-up" size="mini" style="margin-right: 5px"></HintButton>
                    <HintButton @click="edit" title="修改" type="primary" icon="el-icon-edit" size="mini" style="margin-right: 5px"></HintButton>
                    <HintButton @click="getSkuInfo(row)" title="查看" type="info" icon="el-icon-search" size="mini" style="margin-right: 5px"></HintButton>
                    <HintButton title="删除" type="danger" icon="el-icon-delete" size="mini" style="margin-right: 5px"></HintButton>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination @size-change="handleSizeChange" @current-change="getSkuList"
                :current-page="page" :page-sizes="[3, 5, 10]" style="text-align: center"
                :page-size="limit" layout="prev, pager, next, jumper,->,sizes,total" :total="total">
        </el-pagination>

        <!-- 抽屉 -->
        <el-drawer  :visible.sync="drawer" :show-close="false" size="50%">
            <el-row>
                <el-col :span="5">名称</el-col>
                <el-col :span="10">{{skuInfo.skuName}}</el-col>
            </el-row>
            <el-row>
                <el-col :span="5">描述</el-col>
                <el-col :span="10">{{skuInfo.skuDesc}}</el-col>
            </el-row>
            <el-row>
                <el-col :span="5">价格</el-col>
                <el-col :span="10">{{skuInfo.price}}元</el-col>
            </el-row>
            <el-row>
                <el-col :span="5">平台属性</el-col>
                <el-col :span="10">
                    <template>
                        <el-tag style="margin-right: 10px" type="success" v-for="attr in skuInfo.skuAttrValueList" :key="attr.id" size="mini">
                            {{attr.attrId}}-{{attr.valueId}}
                        </el-tag>
                    </template>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="5">商品图片</el-col>
                <el-col :span="10">
                    <el-carousel height="200px">
                        <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                            <img :src="item.imgUrl" alt="" width="100%" height="100%">
                        </el-carousel-item>
                    </el-carousel>
                </el-col>
            </el-row>
        </el-drawer>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                page: 1,
                limit: 5,
                skuList: [],
                total: 0,
                skuInfo: {},
                drawer: false,
            }
        },

        methods: {
            async getSkuList(pager = 1) {
                 this.page = pager;
                 const {code,data} = await this.$API.sku.reqSkuList(this.page,this.limit);
                 if(code === 200) {
                     this.skuList = data.records;
                     this.total = data.total;
                 }
            },

            // 分页
            handleSizeChange(limit) {
                this.limit = limit;
                this.getSkuList();
            },

            // 上架
            async sale(row) {
                const {code} = await this.$API.sku.reqSale(row.id);
                if(code === 200) {
                    this.$message({
                        message: '上架成功',
                        type: 'success'
                    });

                    this.getSkuList();
                }
            },

            // 下架
            async cancel(row) {
                const {code} = await this.$API.sku.reqCancel(row.id);
                if(code === 200) {
                    this.$message({
                        message: '下架成功',
                        type: 'success'
                    });

                    this.getSkuList();
                }
            },

            // 修改
            edit() {
                this.$message({
                    message: '正在开发中',
                    type: 'info'
                });
            },

            // 获取sku详情
            async getSkuInfo(skuInfo) {
                const {code,data} = await this.$API.sku.reqSkuByIdI(skuInfo.id);
                console.log(code,data);
                if(code === 200) {
                    this.skuInfo = data;

                    this.drawer = true;
                }
            }
        },

        mounted() {
            this.getSkuList();
        },

        name: "Sub"
    }
</script>

<style>
    .el-carousel__item h3 {
        color: #475669;
        font-size: 14px;
        opacity: 0.75;
        line-height: 150px;
        margin: 0;
    }

    .el-carousel__item:nth-child(2n) {
        background-color: #99a9bf;
    }

    .el-carousel__item:nth-child(2n+1) {
        background-color: #d3dce6;
    }


</style>

<!--
    scoped
        加上scoped的作用是指示对于当前的组件有用(样式)

        1. 对于莫一个组件,如果style添加上scoped属性,给当前子组件的结构中都添加上一个data-v-xxxx自定义属性
            会发现vue是通过属性选择器,给需要添加的元素添加上样式

            h3[data-v-7ba5bd90]

        2. 子组件的根标签(拥有父组件当中自定义属性:一样的),如果子组件根节点和父组件中书写样式相同,也会添加相应的
            样式.

        3. 如果父组件的样式(scoped),还想影响子组件的样式?像这种情况我们可以使用深度选择器
            深度选择器可以实现样式的穿透
                1. 原生的css >>> 选择器

                2. less     /deep/ 选择器

                3. scss     ::v-deep 选择器

-->
<style scoped>
    .el-row .el-col-5{
        font-size: 18px;
        text-align: right;
    }
    .el-col {
        margin: 10px;
    }

    >>>.el-carousel__button {
        width: 10px;
        height: 10px;
        background-color: red;
        -webkit-border-radius: 50%;
        -moz-border-radius: 50%;
        border-radius: 50%;
    }
</style>
